package net.sshiffon

/**
 *
 * @author amir
 */
public class FileSizeUtil {
	static final def units = [" B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
        static final def blockSize = 1024

    public static String format(long size, int sigFigs)
    {
        long s2= size
        def out=''
        if(s2 < 0)
        {
            out= '-'
            s2 *= -1
        }
        
        int i = 0;
        double d = s2;

        while( d > blockSize - 1 && i < units.size())
        {
            d = d / blockSize
            i++;
        }

        //sigFigs
        long l = (long) (d * Math.pow(10,sigFigs));
        d = (double) l / Math.pow(10,sigFigs);

        out+= "${d} ${units.get(i)}";
        return out;
    }
}

